package com.demo.systemmonitor.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MemoryUtils {
    private static final String TAG = "MemoryUtils";
    
    public static class MemoryData {
        private float usagePercent;
        private long totalMemory;
        private long usedMemory;
        private long freeMemory;
        private long availableMemory;
        
        public MemoryData(float usagePercent, long totalMemory, long usedMemory, 
                         long freeMemory, long availableMemory) {
            this.usagePercent = usagePercent;
            this.totalMemory = totalMemory;
            this.usedMemory = usedMemory;
            this.freeMemory = freeMemory;
            this.availableMemory = availableMemory;
        }
        
        public float getUsagePercent() { return usagePercent; }
        public long getTotalMemory() { return totalMemory; }
        public long getUsedMemory() { return usedMemory; }
        public long getFreeMemory() { return freeMemory; }
        public long getAvailableMemory() { return availableMemory; }
    }
    
    public static MemoryData getMemoryData() {
        try {
            // 获取系统内存信息
            ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            ActivityManager activityManager = (ActivityManager) 
                android.app.ActivityThread.currentApplication().getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.getMemoryInfo(memoryInfo);
            
            // 获取总内存
            long totalMemory = getTotalMemory();
            long availableMemory = memoryInfo.availMem;
            long usedMemory = totalMemory - availableMemory;
            float usagePercent = (float) usedMemory / totalMemory * 100;
            
            return new MemoryData(usagePercent, totalMemory / 1024 / 1024, 
                                usedMemory / 1024 / 1024, 
                                availableMemory / 1024 / 1024,
                                availableMemory / 1024 / 1024);
                                
        } catch (Exception e) {
            Log.e(TAG, "Error getting memory data", e);
            return null;
        }
    }
    
    private static long getTotalMemory() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader("/proc/meminfo"));
            String line = reader.readLine();
            reader.close();
            
            if (line != null && line.startsWith("MemTotal:")) {
                String[] parts = line.split("\\s+");
                if (parts.length >= 2) {
                    return Long.parseLong(parts[1]) * 1024; // 转换为字节
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Error reading /proc/meminfo", e);
        }
        return 0;
    }
}
